package com.codingvip.sort;

/**
 * @author hansongtao
 */
public class PingFangGenCal {
    public static void main(String[] args) {
        System.out.println(sqrt(4,0.00001));
    }




    public static double sqrt(int t, Double precise) {

        if (t < 0) {
            throw new RuntimeException("Negative number cannot have a sqrt root.");
        }

        //先确定当前数所处的最小整数区间
        int i = 0;
        for ( ; i <= t; i++) {
            if (i * i == t) {
                return i;
            }
            if(i * i > t){
                break;
            }
        }

        //再通过二分法来进行判断检测
        double low = i-1, high = i, prec = precise != null ? precise : 1e-5;
        double middle, squre;
        while (high - low > prec) {
            middle = (low + high) / 2;
            squre = middle * middle;

            if (squre > t) {
                high = middle;
            } else {
                low = middle;
            }
        }
        return (low + high) / 2;
    }


    private static Double calPingFang(int num) {


        int mid = 0;
        Double xiaoshuLength =0.1;
        Double value = 1.0;
        Double mayBeValue = 1.0;
        int count = 0;
        while(true) {
            if (count > 3) {
                return mayBeValue;
            }
            int low=1;
            int high = 9;
            while (low <= high) {
                mid = (low + high) / 2;
                mayBeValue = (mid * xiaoshuLength) + value;
                Double tempValue = mayBeValue * mayBeValue;
                if (tempValue == num) {
                    count = Integer.MAX_VALUE;
                    return mayBeValue;
                } else if (tempValue < num) {

                    low = mid + 1;
                    System.out.println("变更Low:"+low+"---当前小数:"+xiaoshuLength);
                } else {
                    high = mid - 1;
                    System.out.println("变更high:"+high+"---当前小数:"+xiaoshuLength);
                }

            }
            mayBeValue = (mid * xiaoshuLength) + value;
            value = mayBeValue;
            xiaoshuLength = xiaoshuLength / 10;
            count++;
        }

    }
}
